package waskung

import "fmt"

// A adds an anchor element.
func (h *HTMLWriter) A(href string) *TagBrush {
	return h.Tag("a").Attr("href", href)
}

// Body adds a <body> element.
func (h *HTMLWriter) Body() *TagBrush {
	return h.Tag("body")
}

// Br adds a line break element.
func (h *HTMLWriter) Br() *TagBrush {
	return h.Tag("br")
}

// Button adds a <button> element.
func (h *HTMLWriter) Button() *TagBrush {
	return h.Tag("button")
}

// Checkbox adds a labeled checkbox input.
func (h *HTMLWriter) Checkbox(name, label string, checked bool) {
	h.Label().With(func() {
		c := h.Tag("input").Type("checkbox").Name(name)
		if checked {
			c.Attr("checked", "")
		}
		h.Text(label)
	})
}

// Div adds a <div> element.
func (h *HTMLWriter) Div() *TagBrush {
	return h.Tag("div")
}

// Fieldset adds a <fieldset> element.
func (h *HTMLWriter) Fieldset() *TagBrush {
	return h.Tag("fieldset")
}

// Form adds a <form> element.
func (h *HTMLWriter) Form() *TagBrush {
	return h.Tag("form")
}

// H adds a heading element (<h1> to <h6>).
func (h *HTMLWriter) H(level int) *TagBrush {
	if level < 1 || level > 6 {
		h.Error(fmt.Errorf("invalid heading element level: <h%d>", level))
	}
	return h.Tag(fmt.Sprintf("h%d", level))
}

// Head adds a <head> element.
func (h *HTMLWriter) Head() *TagBrush {
	return h.Tag("head")
}

// Header adds a <header> element.
func (h *HTMLWriter) Header() *TagBrush {
	return h.Tag("header")
}

// Hr adds a horizontal rule element.
func (h *HTMLWriter) Hr() *TagBrush {
	return h.Tag("hr")
}

// HTML adds an <html> element, and also an HTML5 doctype declaration if it is
// the first tag added.
func (h *HTMLWriter) Html() *TagBrush {
	if len(h.Bytes()) == 0 {
		h.Doctype("html")
	}
	return h.Tag("html")
}

// I adds an <i> element.
func (h *HTMLWriter) I() *TagBrush {
	return h.Tag("i")
}

// Img adds an image tag, with the specified src attribute.
func (h *HTMLWriter) Img(src string) *TagBrush {
	return h.Tag("img").Attr("src", src)
}

// Input adds an <input> element.
func (h *HTMLWriter) Input(typ, name string, value interface{}) *TagBrush {
	return h.Tag("input").Type(typ).Name(name).Value(value)
}

// Label adds a Label element.
func (h *HTMLWriter) Label() *TagBrush {
	return h.Tag("label")
}

// Legend adds a <legend> element.
func (h *HTMLWriter) Legend(text string) {
	h.Tag("legend").Text(text)
}

// Li adds a <li> element.
func (h *HTMLWriter) Li() *TagBrush {
	return h.Tag("li")
}

// Meta adds a <meta> element.
func (h *HTMLWriter) Meta() *TagBrush {
	return h.Tag("meta")
}

// Nav adds a <nav> element.
func (h *HTMLWriter) Nav() *TagBrush {
	return h.Tag("nav")
}

// Option adds an <option> element. The selected attribute will be set if
// value equals selectedValue.
func (h *HTMLWriter) Option(value, selectedValue, label string) {
	t := h.Tag("option").Attr("value", value)
	if value == selectedValue && value != "" {
		t.Attr("selected", "")
	}
	t.Text(label)
}

// P adds a paragraph element.
func (h *HTMLWriter) P() *TagBrush {
	return h.Tag("p")
}

// Script adds a <script> element, with s as its text content.
func (h *HTMLWriter) Script(s string) {
	h.Tag("script").Text(s)
}

// Scripts adds a <script> element linking to each script in sources.
func (h *HTMLWriter) Scripts(sources ...string) {
	for _, src := range sources {
		h.Tag("script").Attr("src", src)
	}
	h.scripts = append(h.scripts, sources...)
}

// Select adds a <select> element.
func (h *HTMLWriter) Select(name string) *TagBrush {
	return h.Tag("select").Name(name)
}

// Stylesheets adds a link element for each stylesheet in hrefs.
func (h *HTMLWriter) Stylesheets(hrefs ...string) {
	for _, href := range hrefs {
		h.Tag("link").Attr("rel", "stylesheet").Attr("href", href)
	}
	h.styles = append(h.styles, hrefs...)
}

// Span adds a <span> element.
func (h *HTMLWriter) Span() *TagBrush {
	return h.Tag("span")
}

// Style adds a <style> element.
func (h *HTMLWriter) Style(text string) {
	h.Tag("style").Text(text)
}

// Table adds a <table> element.
func (h *HTMLWriter) Table() *TagBrush {
	return h.Tag("table")
}

// Tbody adds a <tbody> element.
func (h *HTMLWriter) Tbody() *TagBrush {
	return h.Tag("tbody")
}

// Td adds a <td> element.
func (h *HTMLWriter) Td() *TagBrush {
	return h.Tag("td")
}

// Textarea adds a <textarea> element.
func (h *HTMLWriter) Textarea(name string) *TagBrush {
	return h.Tag("textarea").Name(name)
}

// Th adds a <th> element.
func (h *HTMLWriter) Th() *TagBrush {
	return h.Tag("th")
}

// Thead adds a <thead> element.
func (h *HTMLWriter) Thead() *TagBrush {
	return h.Tag("thead")
}

// Title adds a <title> element, with s as its text content.
func (h *HTMLWriter) Title(s string) {
	h.Tag("title").Text(s)
}

// Tr adds a <tr> element.
func (h *HTMLWriter) Tr() *TagBrush {
	return h.Tag("tr")
}

// Ul adds a <ul> element.
func (h *HTMLWriter) Ul() *TagBrush {
	return h.Tag("ul")
}
